Create table in SQL with PRIMARY KEY
In this part we will learn Create table in SQL with PRIMARY KEY? So, tables are created in SQL to store and organize data in a structured manner. They serve as containers for storing records, with each record represented as a row and each attribute as a column. Tables allow for efficient data retrieval through SQL queries, enable the establishment of relationships between entities, support data manipulation operations, and provide data security through access control. In summary, tables are fundamental components of a database system that facilitate data storage, organization, retrieval, manipulation, and security.
Here's a simple explanation with a diagram:
In SQL, tables are like organized containers that hold data. They consist of rows and columns, forming a tabular structure.
Columns represent specific attributes or data fields, such as name, age, or address.
Rows represent individual records or instances within the table, with each row holding data corresponding to the columns.
For example, let's say we have a table named "Employee" with columns such as "Employee_ID," "Name," "Age," and "Department." Each row in the table represents a specific employee with corresponding data for each column.
By creating tables in SQL, we can structure and store data in a meaningful way, making it easier to retrieve and manipulate the information based on our needs.
To create a table in SQL, you can use the CREATE TABLE statement. Here's a basic example of how to create a table:
Example
CREATE TABLE TableName (
column1 datatype,
column2 datatype,
column3 datatype,
...
);
- CREATE TABLE is the SQL command used to create a new table.
- TableName is the name you choose for your table. Replace it with the desired name.
- column1, column2, column3, and so on, represent the columns in your table. You can specify the column names according to your requirements.
- datatype represents the data type for each column. Examples of data types include INT for integers, VARCHAR for variable-length strings, DATE for dates, and so on.
Example
CREATE TABLE Employees (
EmployeeID INT,
FirstName VARCHAR(50),
LastName VARCHAR(50),
HireDate DATE
);
- EmployeeID is an integer column.
- FirstName and LastName are both variable-length string columns with a maximum length of 50 characters.
- HireDate is a date column.
Example
CREATE TABLE Employees (
EmployeeID INT PRIMARY KEY,
FirstName VARCHAR(50),
LastName VARCHAR(50),
HireDate DATE
);
Can We use primany key more than one?
Example
CREATE TABLE MyTable (
id INT PRIMARY KEY,
name VARCHAR(50),
age INT
);
Example
CREATE TABLE MyTable (
id INT,
category VARCHAR(50),
PRIMARY KEY (id, category)
);
0 Comments
Post a Comment